package cm.android.mytodoit.view;

import cm.android.mytodoit.R;
import cm.android.mytodoit.controller.Todo_SQLOpenHelper;
import cm.android.mytodoit.model.BuyElement;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class GenerateBuyView extends Activity {

	private EditText name, quantity, where;
	private SQLiteDatabase database;
	private Todo_SQLOpenHelper sqlOpenHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.generatebuyview);

		this.name = (EditText) findViewById(R.id.et_generatebuy_name);
		this.quantity = (EditText) findViewById(R.id.et_generatebuy_quantity);
		this.where = (EditText) findViewById(R.id.et_generatebuy_where);
	}

	public void onButtonClick(View view) {
		
		switch (view.getId()) {
		
		case R.id.bu_generatebuy_saveButton:
			
			if ((!(name.getText().toString().equals("")))
					&& (!(quantity.getText().toString().equals("")))) {
			try {
								
						this.sqlOpenHelper = new Todo_SQLOpenHelper(this);
						this.database = sqlOpenHelper.getWritableDatabase();
				
				ContentValues values = new ContentValues();
				
				values.put(BuyElement.TC_NAME, this.name.getText().toString());
				values.put(BuyElement.TC_QUANTITY, this.quantity.getText().toString());
				values.put(BuyElement.TC_INFO, this.where.getText().toString());
				
				database.insert(BuyElement.TABLE_NAME, null, values);
				
				Toast.makeText(this, "new Buy insert", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {

				Toast.makeText(this, "insert Failed", Toast.LENGTH_SHORT).show();
			}
			
			this.finish();
			}
			break;

		}
	}
}
